package ch.ethz.fcl.user.interaction;

import ch.ethz.fcl.udp.Protocal;
import ch.ethz.fcl.udp.UDPClient;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class TimerListener implements OnTimeChangedListener {
	public static int startingHour = 0;
	public static int startingMin = 0;
	public static int endingHour = 23;
	public static int endingMin = 59;

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		if (view.getTag() != null) {
			if (view.getTag().toString().equals("Start")) {
				System.out.println("Start Hour:" + hourOfDay + " Minute:"
						+ minute);
				if (hourOfDay < endingHour
						|| (hourOfDay == endingHour && minute < endingMin)) {
					startingHour = hourOfDay;
					startingMin = minute;
				}
			}
			if (view.getTag().toString().equals("End")) {
				System.out.println("End Hour:" + hourOfDay + " Minute:"
						+ minute);
				if (hourOfDay > startingHour
						|| (hourOfDay == startingHour && minute > startingMin)) {
					endingHour = hourOfDay;
					endingMin = minute;
				}
			}
		}
		int start = 4 * startingHour + startingMin / 15;
		int end = 4 * endingHour + endingMin / 15;
		if (end >= 10)
			new Thread(new UDPClient("MetroBuzz:Timer:" + start + ":"
					+ end)).run();
		else
			new Thread(new UDPClient("MetroBuzz:Timer:" + start + ":0"
					+ end)).run();
		new Thread(new UDPClient("MetroBuzz:Control:" + Protocal.FINGERUP))
				.run();

	}
}
